#include "BaseApp.h"
#include "Camera.h"
#include "InputState.h"
#include "VertexList.h"

BaseApp::BaseApp():
	temp(0),
	drawBoundingBox(false)
{
	
	setupScene();
}

BaseApp::~BaseApp()
{
	delete m_Camera;
	delete m_VertexList;
}

void BaseApp::setupScene()
{
	glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	m_Camera = new Camera();
	m_VertexList = new VertexList();

	m_VertexList->addVertex(3,3,3);
	m_VertexList->addVertex(-2,0,-3);
	m_VertexList->addVertex(0,-3,0);
}

void BaseApp::updateScene(const InputState& refInputState)
{
	m_Camera->updateCamera(refInputState);
	if (refInputState.isKeyDown(VK_RIGHT)) temp += 0.05f;
	if (refInputState.isKeyDown(VK_LEFT)) temp -= 0.05f;
	vector3f tempvec = vector3f(20*cos(temp),20*sin(temp),5);
	m_Camera->SetPos(tempvec);
}

void BaseApp::drawScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	//glLoadIdentity();

	//gluLookAt(5,5,5,0,0,0,0,0,1);

	glBegin(GL_LINES);
		glColor3ub(255,0,0);
		glVertex3f(0,0,0);
		glVertex3f(1,0,0);

		glColor3ub(0,255,0);
		glVertex3f(0,0,0);
		glVertex3f(0,1,0);

		glColor3ub(0,0,255);
		glVertex3f(0,0,0);
		glVertex3f(0,0,1);
	glEnd();

	if (drawBoundingBox) m_VertexList->drawBoundingBox();

	glColor3ub(255,255,255);
	glBegin(GL_TRIANGLES);
		glVertex3f(m_VertexList->getVertex(0).x, m_VertexList->getVertex(0).y, m_VertexList->getVertex(0).z);
		glVertex3f(m_VertexList->getVertex(1).x, m_VertexList->getVertex(1).y, m_VertexList->getVertex(1).z);
		glVertex3f(m_VertexList->getVertex(2).x, m_VertexList->getVertex(2).y, m_VertexList->getVertex(2).z);
	glEnd();
}

void BaseApp::keyPressed(int vKey)
{
	if (vKey == 'R') temp = 0.0f;
	if (vKey == VK_LBUTTON) drawBoundingBox = !drawBoundingBox;
}